package com.snowflake.ecatalog.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.snowflake.ecatalog.contant.Status;

/**
 * 彩页
 */
// JPA标识
@Entity
@Table(name = "t_ec_profile")
public class Profile extends OpEntity {
	private Template template;
	private String number;
	private String name;
	private String remark;
	private int status;
	private List<User> users;
	private List<Company> companys;

	public Profile() {
		super();
	}

	public Profile(Long id) {
		super();
		this.id = id;
	}

	@ManyToMany(cascade = { CascadeType.REFRESH })
	@JoinTable(name = "t_ec_companyprofiledetail", joinColumns = { @JoinColumn(name = "profile_id") }, inverseJoinColumns = { @JoinColumn(name = "company_id") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	public List<Company> getCompanys() {
		return companys;
	}

	public void setCompanys(List<Company> companys) {
		this.companys = companys;
	}

	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "profiles", fetch = FetchType.LAZY)
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	// JPA 基于template_id列的多对一关系定义
	@ManyToOne
	@JoinColumn(name = "template_id")
	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	
	// 不持久化到数据库
	@Transient
	public String getStatusName() {
		return Status.alias(this.status);
	}
}
